package com.wyvoid.forum.web.tag;

import com.wyvoid.forum.domain.Forum;
import com.wyvoid.forum.util.ForumUtil;
import com.wyvoid.forum.service.IForumService;

import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.JspException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * User: cjp
 * Date: 2007-6-23
 * Time: 7:04:34
 */
public class ForumNavTag extends TagSupport {
    private long forumId;
    private boolean viewLastLink;

    public void setForumId(long forumId) {
        this.forumId = forumId;
    }

    public void setViewLastLink(boolean viewLastLink) {
        this.viewLastLink = viewLastLink;
    }

    public int doStartTag() throws JspException {
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        String ctx = request.getContextPath();
        String bar = "<a href=\"" + ctx + "/index.do\">" + ForumUtil.settings.get("bbname") + "</a>";


        try {
            if (forumId > 0) {
                IForumService forumService = (IForumService) ForumUtil.context.getBean("forumService");
                Forum forum = forumService.getForum(forumId);
                Forum oneDegreeParentForum = null;
                Forum twoDegreeParentForum = null;

                if (forum.getParentId() != 0) {
                    oneDegreeParentForum = forumService.getForum(forum.getParentId());
                    if (oneDegreeParentForum != null && oneDegreeParentForum.getParentId() != 0) {
                        twoDegreeParentForum = forumService.getForum(oneDegreeParentForum.getParentId());
                    }
                }

                if (twoDegreeParentForum != null) {
                    if (twoDegreeParentForum.getParentId() == 0) {
                        bar = bar + getGroupLink(twoDegreeParentForum.getId(), twoDegreeParentForum.getName(), ctx);
                    } else {
                        bar = bar + getLink(twoDegreeParentForum.getId(), twoDegreeParentForum.getName(), ctx);
                    }
                }
                if (oneDegreeParentForum != null) {
                    if (oneDegreeParentForum.getParentId() == 0) {
                        bar = bar + getGroupLink(oneDegreeParentForum.getId(), oneDegreeParentForum.getName(), ctx);
                    } else {
                        bar = bar + getLink(oneDegreeParentForum.getId(), oneDegreeParentForum.getName(), ctx);
                    }
                }
                if (viewLastLink) {
                    bar = bar + getLink(forum.getId(), forum.getName(), ctx);
                } else {
                    bar = bar + "&raquo;" + forum.getName();
                }
            }
            pageContext.getOut().write(bar);
            return SKIP_BODY;
        }
        catch (IOException ioe) {
            throw new JspException(ioe.getMessage());
        }
    }

    private String getLink(long id, String name, String ctx) {
        return "&raquo;<a href=\"" + ctx + "/forum.do?forumId=" + id + "\">" + name + "</a>";
    }

    private String getGroupLink(long id, String name, String ctx) {
        return "&raquo;<a href=\"" + ctx + "/index.do?groupId=" + id + "\">" + name + "</a>";
    }
}
